package com.system.userserver.service.imp;

import com.system.commonutils.bean.PagesBean;
import com.system.commonutils.pojo.UserBean;
import com.system.commonutils.pojo.UserInfo;
import com.system.userserver.bean.Account;
import com.system.userserver.bean.Admin;
import com.system.userserver.bean.UserDetail;
import com.system.userserver.bean.UserItem;
import com.system.userserver.mapper.UserMapper;
import com.system.userserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public UserBean selectUserByUsername(String s) {
        UserBean userBean = userMapper.selectUserByUsername(s);
        return userBean;
    }

    @Transactional()
    @Override
    public Boolean addAdminUser(String username, String password, Integer roleId) {
        UserBean userBean = new UserBean();
        userBean.setUser_username(username);
        userBean.setUser_password(new BCryptPasswordEncoder().encode(password));
        userBean.setCreateTime(new Date());
        int i = userMapper.addAdminUser(userBean);
        Integer user_id = userBean.getUser_id();
        int i1 = userMapper.userAddRole(user_id, roleId);
        boolean b = i == 1 && i1 == 1;
        return b;
    }

    @Transactional
    @Override
    public Boolean addUser(String username, String password) {
        UserBean userBean = new UserBean();
        userBean.setUser_username(username);
        userBean.setUser_password(new BCryptPasswordEncoder().encode(password));
        userBean.setCreateTime(new Date());
        int i = userMapper.addAdminUser(userBean);
        Integer user_id = userBean.getUser_id();
        int i1 = userMapper.userAddRole(user_id, 5);
        UserInfo userInfo = new UserInfo();
        userInfo.setUserinfo_userId(user_id);
        String userinfo_nickname = "用户" + username;
        userInfo.setUserinfo_nickname(userinfo_nickname);
        userInfo.setUserinfo_headImg("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3557351520,3025082445&fm=26&gp=0.jpg");
        userInfo.setUserinfo_yqcode(UUID.randomUUID().toString().replace("_",""));
        userInfo.setUserinfo_statue(0);
        int i3 = userMapper.addUserInfo(userInfo);
        Account account = new Account();
        account.setAccount_userId(user_id);
        account.setAccount_integral(0);
        account.setAccount_money(0.0);
        int i2 = userMapper.addAcount(account);

        return i==1&&i1==1&&i3==1&&i2==1;
    }

    @Override
    public PagesBean<Admin> adminList(Integer page, Integer size) {
        PagesBean<Admin> adminPagesBean = new PagesBean<>();
        adminPagesBean.setNowsPage(page);
        adminPagesBean.setPageSize(size);

        List<Admin> admins = userMapper.adminList((page - 1) * size, size);
        Integer integer = userMapper.adminCount();
        adminPagesBean.setData(admins);
        adminPagesBean.setDataCount(integer);

        return adminPagesBean;
    }

    @Override
    public PagesBean<UserItem> userList(Integer page, Integer size, Integer userId, String userNickName, String phone) {

        PagesBean<UserItem> adminPagesBean = new PagesBean<>();
        adminPagesBean.setNowsPage(page);
        adminPagesBean.setPageSize(size);

        List<UserItem> userItems = userMapper.userList((page-1)*size, size,userId,userNickName,phone);

        Integer integer = userMapper.userCount(userId,userNickName,phone);
        adminPagesBean.setData(userItems);
        adminPagesBean.setDataCount(integer);

        return adminPagesBean;
    }

    @Override
    public UserDetail selectUserInfo(Integer userId) {
        UserDetail userDetail = userMapper.selectUserInfo(userId);
        return userDetail;
    }

    @Override
    public Boolean updateUserStatue(Integer userId, Integer statue) {
        Integer integer = userMapper.updateUserStatue(userId, statue);
        return integer==1;
    }

}
